package com.cyw.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class IPUtils {

    private final static String[] headers = new String[]{
            "X-Real-IP",
            "X-Forwarded-For",
    };
    private final static Map<String, String> map = new HashMap<>();

    static {
        map.put("X-Real-IP", "X-Real-IP");
        map.put("X-Forwarded-For", "X-Forwarded-For");
    }

    public static String getIp(HttpServletRequest request) {
        for (String header : headers) {
            String requestHeader = request.getHeader(header);
            if (requestHeader != null) return requestHeader;
        }
        return request.getRemoteAddr();
    }


}